import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/service_execute/service_execute_list_page.dart';
import 'package:ls_work/widgets/dotted_line.dart';
import 'package:ls_work/widgets/my_sliver_delegate.dart';

import '../../viewmodel/service_execute/service_execute_home_view_model.dart';

class ServiceExecuteHomePage extends BaseStatefulMvvmWidget {
  final String? scanCode;
  final String? customerId;
  bool showOnlyFinish = false;
  String title;

  ServiceExecuteHomePage(
      {this.scanCode,
      this.customerId,
      this.showOnlyFinish = false,
      this.title = "服务执行"});

  @override
  State<StatefulWidget> createState() => _ServiceExecuteHomeState();
}

class _ServiceExecuteHomeState
    extends BaseMvvmState<ServiceExecuteHomeViewModel, ServiceExecuteHomePage> {
  List<PickerDataWithIdModel> get titles => widget.showOnlyFinish
      ? [
          PickerDataWithIdModel(pickName: '已完成', pickId: '1'),
        ]
      : [
          PickerDataWithIdModel(pickName: '未完成', pickId: '0'),
          PickerDataWithIdModel(pickName: '已完成', pickId: '1'),
        ];

  @override
  Widget buildLoadingContentView() {
    return DefaultTabController(
        length: titles.length,
        child: NestedScrollView(
            headerSliverBuilder: (context, bool) {
              return [
                SliverToBoxAdapter(
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              CommonText(viewModel.oldData?.elderName ?? "",
                                  textSize: 17, fontWeight: FontWeight.bold),
                              Container(
                                height: 8,
                              ),
                              CommonText(
                                  '${viewModel.oldData?.bedNo ?? ""}  ${viewModel.oldData?.nurseLevel ?? ""}',
                                  textSize: 14),
                            ],
                          ),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              CommonText(
                                  (viewModel.oldData?.taskCount ?? 0)
                                      .toString(),
                                  textSize: 15),
                              Container(
                                height: 8,
                              ),
                              CommonText('总数', textSize: 15)
                            ],
                          ),
                          DottedLine(
                            direction: Axis.vertical,
                            dashWidth: 2,
                          ),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              CommonText(
                                  (viewModel.oldData?.finishTaskCount ?? 0)
                                      .toString(),
                                  textSize: 15),
                              Container(
                                height: 8,
                              ),
                              CommonText('已服务', textSize: 15)
                            ],
                          ),
                          DottedLine(
                            direction: Axis.vertical,
                            dashWidth: 2,
                          ),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              CommonText(
                                  (viewModel.oldData?.waitTaskCount ?? 0)
                                      .toString(),
                                  textSize: 15),
                              Container(
                                height: 8,
                              ),
                              CommonText('待服务', textSize: 15)
                            ],
                          ),
                        ],
                      ).addToContainer(
                          padding:
                              EdgeInsets.symmetric(horizontal: 10, vertical: 8))
                    ],
                  ).addToContainer(
                      width: double.infinity,
                      height: 80,
                      margin:
                          EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                      decoration: BoxDecoration(
                          color: Colors.white, borderRadius: 8.borderRadius)),
                ),
                if (!widget.showOnlyFinish)
                  SliverPersistentHeader(
                      pinned: true,
                      delegate: MySliverDelegate(
                          maxHeight: 50,
                          minHeight: 50,
                          child: Material(
                            color: Colors.white,
                            child: TabBar(
                              labelColor: Colours.app_main,
                              unselectedLabelColor: Colors.black,
                              indicatorColor: Colours.app_main,
                              indicatorSize: TabBarIndicatorSize.label,
                              tabs: titles
                                  .map((e) => Tab(
                                        text: e.pickName,
                                      ))
                                  .toList(),
                            ),
                          )))
              ];
            },
            body: viewModel.oldData == null
                ? Container()
                : TabBarView(
                    children: titles
                        .map((e) => ServiceExecuteListFragment(
                              elderCode: widget.scanCode,
                              status: e.pickId!,
                              customerElderId: widget.customerId,
                              canBatch: viewModel.isBatchClick,
                            ))
                        .toList(),
                  )));
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Color(0xfff1f1f1),
      appBar: AppBar(
        title: Text(widget.title),
        actions: [
          widget.showOnlyFinish
              ? Container()
              : CommonText((vm?.isBatchClick ?? false) ? "取消" : "批量执行",
                      textColor: Colours.app_main)
                  .addToContainer(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(right: 16))
                  .onTap(() {
                  setState(() {
                    viewModel.batchClick();
                  });
                })
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  ServiceExecuteHomeViewModel createViewModel() {
    return ServiceExecuteHomeViewModel(context,
        scanCode: widget.scanCode, customerElderId: widget.customerId);
  }

  @override
  void onRetryClick() {}
}
